define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ques/school/index',
                    add_url: 'ques/school/add/ids/'+volume_ids,
                    edit_url: 'ques/school/edit',
                    del_url: 'ques/school/del',
                    multi_url: 'ques/school/multi',
                    table: 'ques_volume_school',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='school_id']", form).addClass("selectpage").data("source", "base/school/index").data("primaryKey", "id").data("field", "name",'id').data("orderBy", "id desc");
                $("input[name='book_id']", form).addClass("selectpage").data("source", "base/book/index").data("primaryKey", "id").data("field", "name",'id').data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                 queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {}; //判断当前是否还有其他高级搜索栏的条件
                    var op = params.op ? JSON.parse(params.op) : {}; 
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    filter.volume_id = volume_ids;
                    op.volume_id = "=";
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'school_text', title: __('School_id'), operate:false},
                        {field: 'book_text', title: __('Book_id'), operate:false},
                         {field: 'school_id', title: __('School_id'), visible: false,formatter: Table.api.formatter.search},
                          {field: 'book_id', title: __('Book_id'),operate:"FINDINSET", visible: false,formatter: Table.api.formatter.search},
                        {field: 'publish_text', title: __('Publish')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                       
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
          require(['selectpage'], function () {
            $('#c-school_id').selectPage({
                 eAjaxSuccess: function (data) {
                data.list = typeof data.rows !== 'undefined' ? data.rows : (typeof data.list !== 'undefined' ? data.list : []);
                data.totalRow = typeof data.total !== 'undefined' ? data.total : (typeof data.totalRow !== 'undefined' ? data.totalRow : data.list.length);
                return data;
              },
              eSelect : function(data){
                console.log(data);
                $.ajax({
                  url: 'ajax/getSchoolBook/id/'+data.id,
                  dataType: 'json',
                  success: function (res) {
                      $('#c-book_id').val(res.bookname);
                      $('#c-book_id').selectPageRefresh();
                  }
                });
               
              }
            });
          });
        },
        edit: function () {
            Controller.api.bindevent();
            require(['selectpage'], function () {
            $('#c-school_id').selectPage({
                 eAjaxSuccess: function (data) {
                data.list = typeof data.rows !== 'undefined' ? data.rows : (typeof data.list !== 'undefined' ? data.list : []);
                data.totalRow = typeof data.total !== 'undefined' ? data.total : (typeof data.totalRow !== 'undefined' ? data.totalRow : data.list.length);
                return data;
              },
              eSelect : function(data){
                console.log(data);
                $.ajax({
                  url: 'ajax/getSchoolBook/id/'+data.id,
                  dataType: 'json',
                  success: function (res) {
                      $('#c-book_id').val(res.bookname);
                      $('#c-book_id').selectPageRefresh();
                  }
                });
               
              }
            });
          });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});